/**
 * 
 */
package moviles.turnomovil.interfaz;

import moviles.app.mundo.R;
import moviles.turnomovil.mundo.Alerta;
import moviles.turnomovil.mundo.TurnoMovil;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

/**
 * @author juanjo
 *
 */
public class TomarTurnoWifiActivity extends Activity {

	private Spinner spSelect;
	
	private ArrayAdapter<String> adapterProcesos;
	
	private String procesoSeleccionado;
	
	private Button btnTomarTurno;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lytomarturnowifi);
		
		spSelect = (Spinner) findViewById(R.id.spProcesos);
		
		
		adapterProcesos = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
		
		//TODO: Pedir procesos del servidor HTTP
		
		
		
		try {
			
			URL serverAddress = new URL("http://localhost:8080/TurnoMovil/DarProcesos");
			HttpURLConnection connection = (HttpURLConnection)serverAddress.openConnection();
	        connection.setRequestMethod("GET");
	        connection.setDoOutput(true);
	        connection.setReadTimeout(10000);
	                    
	        connection.connect();
	        
	        BufferedReader  rd  = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	        String ln = rd.readLine();
	        
			String[] procesos = ln.split(",");
			

			for( int i= 0; i < procesos.length ; i++ )
			{
				if(procesos[i] != null )
					adapterProcesos.add(procesos[i]);
			}
			
			connection.disconnect();

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		spSelect.setAdapter(adapterProcesos);
		
		btnTomarTurno = (Button)findViewById(R.id.btnSelectTurno);
		btnTomarTurno.setOnClickListener( new View.OnClickListener() {
			
			public void onClick(View v) {
				tomarTurno();
			}
		});

	}
	
	protected void tomarTurno() {
		// TODO Auto-generated method stub
		
	}
	
//	/**
//	 * 
//	 */
//	public TomarTurnoWifiActivity() {
//		// TODO Auto-generated constructor stub
//	}

}
